import com.iwat.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangweiqiang
 * @version 1.0
 * @date 2021-08-17 9:14
 */
public class ProcessVariable {

    @Test
    public void testDeploy(){
        final ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        final RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evection-global.bpmn") // 添加bpmn资源
                .name("全局变量测试")
                .disableSchemaValidation()//禁用验证
                .deploy();

        System.out.println(deployment.getKey());
    }


    @Test
    public void testStartProcee(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final RuntimeService runtimeService = processEngine.getRuntimeService();

        Evection evection = new Evection();
        evection.setNum(1d);
        //创建流程实例是动态指定处理人
        Map<String,Object> assigneeMap = new HashMap<>();
        assigneeMap.put("evection",evection);
        assigneeMap.put("assignee0","张三");
        assigneeMap.put("assignee1","李经理");
        assigneeMap.put("assignee2","王总经理");
        assigneeMap.put("assignee3","赵财务");

        final ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myEvection2", assigneeMap);


        System.out.println(processInstance.getId());
    }

    @Test
    public void testCompeletTaskWithGlobalVariable(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final TaskService taskService = processEngine.getTaskService();
        final RuntimeService runtimeService = processEngine.getRuntimeService();


        final Task task = taskService.createTaskQuery().processInstanceId("125001").singleResult();
        System.out.println("===================================================================");

        System.out.println(taskService.getVariable(task.getId(), "evection", Evection.class));

        System.out.println("===================================================================");

        //张三完成
        if("张三".equals(task.getAssignee())){

            /*
             * 1.通过当前流程实例设置流程变量
             *  通过当前流程实例设置流程变量，下一级任务由王总经理办理
             *  注意：executionId必须当前未结束 流程实例的执行id
             *  执行代码：runtimeService.setVariable("100001","assignee2","王总经理");
             *
             * 2.通过当前任务设置
             *  通过当前流程实例设置流程变量，下一级任务由王总经理办理
             *  注意：任务id必须是当前待办任务id，act_ru_task中存在。如果该任务已结束，会报错
             *  执行代码：taskService.setVariable(task.getId(),"assignee2","王总经理");
             */

            taskService.setVariable(task.getId(),"assignee1","王总经理");
            //runtimeService.setVariable("100001","assignee2","王总经理");

            taskService.complete(task.getId());
            System.out.println("张三审核完成！！！");
        }else if("李经理".equals(task.getAssignee())){

            //任务办理时设置变量流程变量，指定下一级任务由业务员办理
            final HashMap<String, Object> map = new HashMap<>();
            map.put("assignee2","业务员");

            taskService.complete(task.getId(),map);
            System.out.println("李经理审核完成！！！");
        }else if("王总经理".equals(task.getAssignee())){

            taskService.complete(task.getId());
            System.out.println("王总经理审核完成！！！");
        }else if("赵财务".equals(task.getAssignee())) {

            taskService.complete(task.getId());
            System.out.println("赵财务审核完成！！！");
        }else if("业务员".equals(task.getAssignee())){

            taskService.complete(task.getId());
            System.out.println("业务员审核完成！！！");
        }
    }

    @Test
    public void testLocalVariable(){

        /*
            任务办理时设置local流程变量，当前运行的流程实例只能在该任务结束前使用，
            任务结束该变量无法在当前流程实例使用，可以通过查询历史任务查询。
         */
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        HistoryService historyService = processEngine.getHistoryService();
        final List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId("52501").list();


        Task task = taskService.createTaskQuery().processInstanceId("52501").singleResult();
        //张三完成
        if("张三".equals(task.getAssignee())){
            HashMap<String, Object> map = new HashMap<>();

            /*
                尝试指定assignee1为业务员失败
                原因：Local变量在任务结束后无法在当前流程实例执行中使用，如果后续的流程执行需要用到此变量则会报错。
             */
            map.put("assignee1","业务员");
            taskService.setVariablesLocal(task.getId(),map);

            taskService.complete(task.getId());
            System.out.println("张三审核完成！！！");
        }else if("李经理".equals(task.getAssignee())){

            taskService.complete(task.getId());
            System.out.println("李经理审核完成！！！");
        }else if("王总经理".equals(task.getAssignee())){

            taskService.complete(task.getId());
            System.out.println("王总经理审核完成！！！");
        }else if("赵财务".equals(task.getAssignee())) {

            taskService.setVariableLocal(task.getId(),"testVar","树头红叶偏偏，疏林如画");

            taskService.complete(task.getId());
            System.out.println("赵财务审核完成！！！");
        }else if("业务员".equals(task.getAssignee())){

            taskService.complete(task.getId());
            System.out.println("业务员审核完成！！！");
        }else {

            for (HistoricTaskInstance historicTaskInstance : list) {
                //查询历史变量
                System.out.println(historicTaskInstance.getTaskLocalVariables());
            }

        }
    }


    @Test
    public void testHistoryLocalVariable(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId("52501").list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("==============================");
            System.out.println("任务id：" + historicTaskInstance.getId());
            System.out.println("任务名称：" + historicTaskInstance.getName());
            System.out.println("任务负责人：" + historicTaskInstance.getAssignee());
            System.out.println("任务local变量："+ historicTaskInstance.getTaskLocalVariables().toString());

        }
    }
}
